/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.gruposicom.controller;

import ar.com.gruposicom.domain.Command;
import ar.com.gruposicom.domain.Operation;
import ar.com.gruposicom.domain.Politic;
import ar.com.gruposicom.logic.ResultAnalizer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;

/**
 *
 * @author marcelo
 */
public class AnalizerController 
{
    private Timer timer;
    private Command com;
    private Collection<Operation> operations;

    public AnalizerController() 
    {
    }
    
    public Timer initSecuency(final SystemOut salida, final Collection<Operation> operations, String minutos, String segundos)
    {
        final int tiempo = this.convertirTiempo(minutos, segundos);
        final ResultAnalizer re = new ResultAnalizer();
//        final Ping ping = new Ping();
//        Collection<String> salida = new ArrayList<>();
        timer = new Timer (tiempo, new ActionListener () 
        { 
            @Override
            public void actionPerformed(ActionEvent e) 
            { 
                
                Iterator<Operation> it = operations.iterator();
                while(it.hasNext())
                {
                    Operation o = it.next();
                    Iterator<Command> c = o.getCommand().iterator();
                    while(c.hasNext())
                    {
                        Command co = null;
                        try 
                        {
                             co = c.next();
                            ExecuteCommand exe = new ExecuteCommand(co, salida);
                            exe.start();
                            Thread.sleep(10000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(AnalizerController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        
//                        Iterator<Politic> pol = co.getPolitics().iterator();
//                        while(pol.hasNext())
//                        {
//                            Politic p = pol.next();
//                            System.out.println("");
//                            System.out.println("Analizando politica: "+p.getName().trim());
////                            boolean band = re.getPoliticState(salida.getCommand().getRes(), p);
////                            System.out.println("Las respuestas tiene ")
//                            boolean band = re.getPoliticState(getCom().getRes(), p);
//                            System.out.println("El resultado es: "+band);
//                        }
                    }
                }
            } 
        }); 
        
        timer.start();
        return timer;
    }
    
    public Timer initSecuency(final SystemOut salida, String minutos, String segundos)
    {
        OperationController controllerOp = new OperationController();
        //si el metodo getAll() devuelve un null entonces no se pudo establecer la conexion con la base de datos.
        //autmaticamente deberia de leer el csv con las operaciones a ejecutar.
        //en cabio, si devuelve una coleccion con elementos, esa coleccion se deberia guardar en un csv
        //como respaldo por si se cae el servidor en algun momento.
        this.operations = controllerOp.getAll();
        final int tiempo = this.convertirTiempo(minutos, segundos);
        final ResultAnalizer re = new ResultAnalizer();
//        final Ping ping = new Ping();
//        Collection<String> salida = new ArrayList<>();
        timer = new Timer (tiempo, new ActionListener () 
        { 
            @Override
            public void actionPerformed(ActionEvent e) 
            { 
                Iterator<Operation> it = operations.iterator();
                while(it.hasNext())
                {
                    Operation o = it.next();
                    Iterator<Command> c = o.getCommand().iterator();
                    while(c.hasNext())
                    {
                        Command co = null;
                        try 
                        {
                             co = c.next();
                            ExecuteCommand exe = new ExecuteCommand(co, salida);
                            exe.start();
                            Thread.sleep(10000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(AnalizerController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            } 
        }); 
        
        timer.start();
        return timer;
    }
    
    public boolean analizeCommand(Command c)
    {
        boolean band = false;
        ResultAnalizer analizer = new ResultAnalizer();
        Iterator<Politic> it = c.getPolitics().iterator();
//        Iterator<Result> itRes = c.getResults().iterator();
        while(it.hasNext())
        {
            if(analizer.getPoliticState(c.getRes(), it.next()))
            {
                band = true;
            }
        }
        return band;
    }
    
    private int convertirTiempo(String minutos, String segundos)
    {
        int min = Integer.parseInt(minutos);
        int seg = Integer.parseInt(segundos);
        seg = seg+(min*60);
        return seg*1000;
    }

    public Command getCom() {
        return com;
    }

    public void setCom(Command com) {
        this.com = com;
    }

    public Timer getTimer() {
        return timer;
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }
    
    
    
}
